import { sendCode } from '@/api/auth'

export default {
  data: () => ({
    captcha: {
      canSend: 0,
      key: null,
      expired_at: null,
    },
    phone: null,
    timer: null,
  }),
  methods: {
    decrementSendCodeTime() {
      this.captcha.canSend = 60
      this.timer = setInterval(() => {
        if (this.captcha.canSend <= 0) {
          clearInterval(this.timer)
        } else {
          this.captcha.canSend--
        }
      }, 1000)
    },
    async doSendCode() {
      // 验证手机号
      let Mobile = this.phone

      if (Mobile === '' || Mobile === 0) {
        this.$toast.fail('请填写手机号码')
        return
      } else if (!/^1[3456789]\d{9}$/.test(Mobile)) {
        this.$toast.fail('手机号码格式不正确')
        return
      }
      // let sendData = {
      //   phone: Mobile,
      // }
      try {
        const { code, msg, ExpireTime } = await sendCode(Mobile)
        if (code === 0) {
          this.decrementSendCodeTime()
          this.$toast.success(msg)
          this.captcha.expired_at = ExpireTime
          this.captcha.key = true
        } else {
          this.$toast.fail(msg)
        }
      } catch (e) {
        this.$toast.fail(e)
      }
      return
    },
  },
}
